﻿Public Class clsFunctions
    Public Shared Function ApplicationName() As String
        Return My.Application.Info.ProductName
    End Function


    Public Shared Sub SortRefueling(ByRef pRefuelingList As List(Of clsRefueling))
        Dim pNewList As New List(Of clsRefueling)

        Dim pLowestDate As Date = Nothing
        Dim pNextEntry As clsRefueling = Nothing

        Do While pRefuelingList.Count > 0
            pLowestDate = Nothing

            For Each pEntry In pRefuelingList
                If pLowestDate = Nothing Then
                    pLowestDate = pEntry.RefuelDate
                    pNextEntry = pEntry
                ElseIf pLowestDate > pEntry.RefuelDate Then
                    pLowestDate = pEntry.RefuelDate
                    pNextEntry = pEntry
                End If
            Next

            pNewList.Add(pNextEntry)

            pRefuelingList.Remove(pNextEntry)
        Loop

        pRefuelingList = pNewList
    End Sub

    Public Shared Function GetUserAppFolder() As String
        Dim sUserFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
        Dim sUserAppPath As String = IO.Path.Combine(sUserFolder, "HendrichApplications", ApplicationName)

        Try
            IO.Directory.CreateDirectory(sUserAppPath)
        Catch ex As Exception
        End Try

        Return sUserAppPath
    End Function

    Public Shared Function GetYearOfFirstEntry(ByVal pRefuelList As List(Of clsRefueling))
        Dim iFirstYear As Integer = -1

        For Each pEntry In pRefuelList
            If iFirstYear = -1 Or pEntry.RefuelDate.Year < iFirstYear Then
                iFirstYear = pEntry.RefuelDate.Year
            End If
        Next

        Return iFirstYear
    End Function
    Public Shared Function GetYearOfLastEntry(ByVal pRefuelList As List(Of clsRefueling))
        Dim iLastYear As Integer = -1

        For Each pEntry In pRefuelList
            If iLastYear = -1 Or pEntry.RefuelDate.Year > iLastYear Then
                iLastYear = pEntry.RefuelDate.Year
            End If
        Next

        Return iLastYear
    End Function

    Public Shared Function FormatCurrency(ByVal dValue As Single) As String
        Return FormatCurrency(dValue, 2)
    End Function
    Public Shared Function FormatCurrency(ByVal dValue As Single, ByVal iDecimal As Integer) As String
        Return FormatUnit(dValue, "€", iDecimal)
    End Function
    Public Shared Function FormatUnit(ByVal dValue As Single, ByVal sUnit As String) As String
        Return FormatUnit(dValue, sUnit, 2)
    End Function
    Public Shared Function FormatUnit(ByVal dValue As Single, ByVal sUnit As String, ByVal iDecimal As Integer) As String
        Return FormatDecimal(dValue, iDecimal) & " " & sUnit
    End Function
    Public Shared Function FormatDecimal(ByVal dValue As Single)
        Return FormatDecimal(dValue, 2)
    End Function
    Public Shared Function FormatDecimal(ByVal dValue As Single, ByVal iDecimal As Integer) As String
        Dim sIn As String = Math.Round(dValue, iDecimal).ToString.Replace(".", ",")
        Dim sPreDecimal As String = sIn.Split(",")(0)
        Dim sPastDecimal As String = String.Empty

        If sIn.Split(",").Count >= 2 Then
            sPastDecimal = sIn.Split(",")(1)
        End If

        Do Until sPastDecimal.Count = iDecimal
            sPastDecimal &= "0"
        Loop

        Return sPreDecimal & "," & sPastDecimal
    End Function

End Class
